﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using Web.Helper;

namespace Web.Areas.Manager.Controllers
{
    [UserAuthorize]
    public class DueFundController : Controller
    {
   //    DueFund_BLL _DueFund_BLL = new DueFund_BLL();
       Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
       Supplier_BLL _Supplier_BLL = new Supplier_BLL();
       BuyStock_BLL _BuyStock_BLL = new BuyStock_BLL();
        private M_DueFund_BLL _M_DueFund_BLL = new M_DueFund_BLL();
        private ProductCatalog_BLL _ProductCatalog_BLL = new ProductCatalog_BLL();
       
          public virtual ActionResult Index(int? pageIndex, string keyword)
        {
            var model = _BuyStock_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
              //  model = model.Where(a => a.UserName.Contains(keyword));
            }
            return View(model.ToPagedList(pageIndex));
        }


        public virtual ActionResult Details(Guid Id)

        {
            var item = _BuyStock_BLL.Get(Id);
            return View(item);
        }

        public virtual ActionResult Edit(Guid? Id)
        {


            IList<M_DueFund> list = new List<M_DueFund>();
            ViewData["DueFund"] = list;

            ViewData["Supplier"] = _Supplier_BLL.Get();

            ViewData["ProductCatalog"] = _ProductCatalog_BLL.Get();
            ViewData["Sys_Users"] = _Sys_Users_BLL.Get();
            var item = _BuyStock_BLL.Get(Id);
             
            return View(item);
        }

         [HttpPost]
       
         public virtual ActionResult Edit(Guid? Id, BuyStock collection)
        {
            IList<M_DueFund> list = new List<M_DueFund>();

            string[] Number = Request["number1"].Split(',');
            string[] DueMoney = Request["DueMoney"].Split(',');
           // string[] IsForm = Request["IsForm"].Split(',');
            string[] Description = Request["Description1"].Split(',');
            for (int i = 0; i < Number.Length; i++)
            {
                M_DueFund item1 = new M_DueFund();
                item1.Id = Guid.NewGuid();
                item1.ItemId = collection.Id;
                item1.Number = string.IsNullOrEmpty(Number[i]) ? "" : Number[i];
                item1.Description = string.IsNullOrEmpty(Description[i]) ? "" : Description[i];
                // ReSharper disable SimplifyConditionalTernaryExpression
              //  item1.IsForm =bool.Parse(IsForm[i] )== false?false:true;
                // ReSharper restore SimplifyConditionalTernaryExpression
                item1.DueMoney = decimal.Parse(String.IsNullOrEmpty(DueMoney[i]) || !StringExt.IsPrice(DueMoney[i]) ? "0" : DueMoney[i]);
                item1.DateTime = DateTime.Now;
                list.Add(item1);
                if (string.IsNullOrEmpty(Number[i]))
                {
                    ModelState.AddModelError("Number", "请填写付款期次编号");
                }
                if (string.IsNullOrEmpty(DueMoney[i]) || !StringExt.IsPrice(DueMoney[i]))
                {
                    ModelState.AddModelError("DueMoney", "填写实付款金额不正确");
                }
            }
            ViewData["DueFund"] = list;
            if (!ModelState.IsValid)
            {
                ViewData["Supplier"] = _Supplier_BLL.Get();
                ViewData["ProductCatalog"] = _ProductCatalog_BLL.Get();
                ViewData["Sys_Users"] = _Sys_Users_BLL.Get();
                return View(collection);
            }
            IQueryable<M_DueFund> lists = _BuyStock_BLL .Get(Id).M_DueFund.AsQueryable();
            foreach (var mReceiveForm in lists)
            {
                _M_DueFund_BLL.Delete(mReceiveForm.Id);
            }
            for (int i = 0; i < Number.Length; i++)
            {
                M_DueFund item1 = new M_DueFund();
                item1.Id = Guid.NewGuid();
                item1.ItemId = collection.Id;
                item1.Number = string.IsNullOrEmpty(Number[i]) ? "" : Number[i];
              //  item1.IsForm = bool.Parse(IsForm[i]) ==false?false:true;
                item1.DueMoney =
                decimal.Parse(String.IsNullOrEmpty(DueMoney[i]) || !StringExt.IsPrice(DueMoney[i])
                                      ? "0"
                                      : DueMoney[i]);
                item1.DateTime = DateTime.Now;
                item1.Description = string.IsNullOrEmpty(Description[i]) ? "" : Description[i];
                item1.UserId = new Guid(User.Identity.Name);
                list.Add(item1);
                _M_DueFund_BLL.Save(null,ref item1);
            }
            return RedirectToAction("Index",MyrouteValues.Get());
        }
        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        {
            var item = _BuyStock_BLL.Get(Id);
            foreach (var item1 in item.M_DueFund)
            {
                _M_DueFund_BLL.Delete(item1.Id);
            }  

            return RedirectToAction("Index",MyrouteValues.Get());
        }
    }
}